import 'package:flutter/material.dart';
import '../../utils/localization_util.dart';
import '../common/custom_picker.dart';
import '../common/picker_item.dart'; // [MODIFIED] 引入新的 PickerItem

class EditTimePicker extends StatefulWidget {
  final DateTime initialDate;
  final bool showLaterTime;
  final Locale locale;
  final ValueChanged<Map<String, int>> onDateChanged;

  const EditTimePicker({
    super.key,
    required this.initialDate,
    required this.showLaterTime,
    required this.onDateChanged,
    required this.locale,
  });

  @override
  State<EditTimePicker> createState() => _EditTimePickerState();
}

class _EditTimePickerState extends State<EditTimePicker> {
  late int _selectedHour;
  late int _selectedMinute;
  final DateTime _now = DateTime.now();
  late final PickerLocalizations l10n;

  @override
  void initState() {
    super.initState();
    l10n = PickerLocalizations(widget.locale);
    if (!widget.showLaterTime && widget.initialDate.isAfter(_now)) {
      _selectedHour = _now.hour;
      _selectedMinute = _now.minute;
    } else {
      _selectedHour = widget.initialDate.hour;
      _selectedMinute = widget.initialDate.minute;
    }
  }

  void _notifyChange() {
    widget.onDateChanged({
      "hour": _selectedHour,
      "minute": _selectedMinute,
    });
  }

  @override
  Widget build(BuildContext context) {
    int maxMinute =
    (!widget.showLaterTime && _selectedHour == _now.hour) ? _now.minute : 59;

    return Container(
      height: 120,
      padding: const EdgeInsets.symmetric(horizontal: 32),
      child: Row(
        children: [
          Expanded(
            child: CustomPicker(
              startValue: 0,
              endValue: widget.showLaterTime ? 23 : _now.hour,
              initialValue: _selectedHour,
              onValueChanged: (value) {
                setState(() {
                  _selectedHour = value;
                  if (!widget.showLaterTime &&
                      _selectedHour == _now.hour &&
                      _selectedMinute > _now.minute) {
                    _selectedMinute = _now.minute;
                  }
                });
                _notifyChange();
              },
              // 使用 PickerItem
              itemBuilder: (context, value, isSelected) => PickerItem(
                value: value.toString().padLeft(2, '0'),
                suffix: l10n.hour,
                isSelected: isSelected,
              ),
            ),
          ),
          Expanded(
            child: CustomPicker(
              startValue: 0,
              endValue: maxMinute,
              initialValue: _selectedMinute,
              onValueChanged: (value) {
                setState(() {
                  _selectedMinute = value;
                });
                _notifyChange();
              },
              // 使用 PickerItem
              itemBuilder: (context, value, isSelected) => PickerItem(
                value: value.toString().padLeft(2, '0'),
                suffix: l10n.minute,
                isSelected: isSelected,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
